<?php
/**
 * 73YC mobile首页文件
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/

define('IN_ECS', true);
define('IS_MOBILE', true);

require('includes/init.php');
if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

// if(!$_SESSION['no_first_visit']){
//     $_SESSION['no_first_visit'] = 1;
//     header('location:limit.html');exit;
// }
$string = base64_decode('YTozOntzOjg6ImtleXdvcmRzIjtzOjY6IumprOWPryI7czoxMDoiaW1hZ2VGaWVsZCI7czowOiIiO3M6MTg6InNlYXJjaF9lbmNvZGVfdGltZSI7aToxNDExMzY5OTAyO30=');
$act=!empty($_GET['act'])?$_GET['act']:'1';
$cache_id = sprintf('%X', crc32("mobile_index.dwt" . '-' .$act. '-' . (!empty($_GET['id'])?$_GET['id']:'') . $_CFG['lang']));
if (!$smarty->is_cached('index.dwt', $cache_id))
{

    assign_template();
    $position = assign_ur_here();
    $smarty->assign('page_title',      $position['title']);    // 页面标题
    $smarty->assign('daodansong_list', get_daodansong());
    $smarty->assign('wap_logo', $_CFG['wap_logo']);
    $smarty->assign('footer', get_footer());
    $position = assign_ur_here();

    if($act == 'cate'){
        
        /* 获得请求的分类 ID */
        if (isset($_REQUEST['id']))
        {
            $cat_id = intval($_REQUEST['id']);
            
            /* 从数据库中读取分类 */
            /* $cat = get_cat_info($cat_id); 
            $cat_all = get_3layer_cat($cat);*/

            /* 从缓存中读取分类 */
            $cat_all = get_3layer_cache_cat($cat_id);
            
            $smarty->assign('cat_id',$cat_id);
            $smarty->assign('cat_layer1',$cat_all[1]);
            $smarty->assign('cat_layer2',$cat_all[2]);
        }
        else
        {
            /* 一级分类，即读取与当前分类同级的所有一级分类 */
            /* $cat_layer1 = $GLOBALS['db']->getAll("SELECT `cat_id`, `cat_name`, `keywords`, `cat_desc`, `style`, `grade`, `filter_attr`, `parent_id`, `layer` FROM  {$GLOBALS['ecs']->table('category')} WHERE `parent_id` = 600 ORDER BY `sort_order` DESC");*/
           
            $cat_data = read_static_cache('cat_option_static');
            $cat_layer1 = array();
            foreach ($cat_data as $k => $v)  
            {
                if($v['parent_id']==600 && $v['is_show']) 
                {
                     $cat_layer1[$k] = $v;
                     $sort_order[$k] = $v['cat_id'];
                }
            }
            /* 按二维数据中 sort_order 排序 */
            array_multisort( $sort_order,SORT_ASC ,$cat_layer1);
            
            $smarty->assign('cat_id',0);
            $smarty->assign('cat_layer1',$cat_layer1);
            
            /* $sql = "SELECT * FROM " . $ecs->table('brand') . " ORDER BY sort_order ASC";
            $brand_list = $GLOBALS['db']->getAll($sql);*/
            
            $brand_list = array(
                 1 => array('brand_id'=>1,'brand_name'=>'美邦'),
                 2 => array('brand_id'=>2,'brand_name'=>'辉柏嘉'),
                 3 => array('brand_id'=>3,'brand_name'=>'波普'),
                 4 => array('brand_id'=>4,'brand_name'=>'樱花'),
                 5 => array('brand_id'=>5,'brand_name'=>'雪山'),
                 6 => array('brand_id'=>6,'brand_name'=>'马可'),
                 7 => array('brand_id'=>7,'brand_name'=>'米娅'),
                 8 => array('brand_id'=>8,'brand_name'=>'康颂'),
                 9 => array('brand_id'=>9,'brand_name'=>'马利'),
                 10 => array('brand_id'=>10,'brand_name'=>'凤凰'),
                 11 => array('brand_id'=>11,'brand_name'=>'中华'),
                 12 => array('brand_id'=>12,'brand_name'=>'古风'),
                 13 => array('brand_id'=>13,'brand_name'=>'houch'),
                 14 => array('brand_id'=>14,'brand_name'=>'尊爵'),
                 15 => array('brand_id'=>15,'brand_name'=>'得力'),
                 16 => array('brand_id'=>16,'brand_name'=>'大千'),
                 17 => array('brand_id'=>17,'brand_name'=>'施德楼'),
                 18 => array('brand_id'=>18,'brand_name'=>'三菱'),
                 19 => array('brand_id'=>19,'brand_name'=>'伦勃朗'),
                 20 => array('brand_id'=>20,'brand_name'=>'新概念'),
                 21 => array('brand_id'=>21,'brand_name'=>'温莎'),
                 22 => array('brand_id'=>22,'brand_name'=>'梵高'),
                 23 => array('brand_id'=>23,'brand_name'=>'贝碧欧'),
                 24 => array('brand_id'=>24,'brand_name'=>'高尔乐'),
            );
            
            $smarty->assign('brand_list',$brand_list);
        }  
        
    }

    $ad_1=get_ad(18);//首页轮播广告
    $ad_2=get_ad(19);//首页产品广告
    $ad_3=get_ad(20);//首页广告
    $ad_4=get_ad(21);//首页活动广告
    $ad_5=get_ad(22);//首页分类广告
    $smarty->assign('ad_1', $ad_1);
    $smarty->assign('ad_2', $ad_2);
    $smarty->assign('ad_3', $ad_3);
    $smarty->assign('ad_4', $ad_4);
    $smarty->assign('ad_5', $ad_5);
    //获取当天闪购活动数据
    $time = time();//当前时间
    $day = date("Y-m-d",$time);  //获取当前时间
    $day_now_ten=strtotime($day)+79200;//获取当天22点的时间戳作为开始时间
    $day_now_eleven=strtotime($day)+82800;//获取当天23点的时间戳作为结束时间

    if($time >$day_now_eleven){
        $day_now_ten=strtotime($day)+165600;//获取明天22点的时间戳作为开始时间
        $day_now_eleven=strtotime($day)+169200;//获取明天23点的时间戳作为结束时间
    }
    //如果没有到22点获取活动开始倒计时
    if($day_now_ten>$time){
        $expect_start_time = $day_now_ten-$time;
    }
    //活动开始时候计算剩余结束时间
    if($day_now_ten<$time && $time <$day_now_eleven){
        $expect_end_time = $day_now_eleven-$time;
    }
    //获取闪购商品
    $sql = 'SELECT l.end_time, g.goods_id, g.sku, g.goods_name, g.promote_start_date,g.promote_end_date, g.goods_thumb , g.promote_price,g.market_price ,(l.goods_num-l.buy_num) AS goods_stock ' .
    ' FROM ' . $GLOBALS['ecs']->table('limit') . ' AS l ' .
    ' LEFT JOIN '.$GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = l.goods_id' .
    ' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' .
    " AND l.start_time = $day_now_ten AND l.end_time = $day_now_eleven AND l.end_time = g.promote_end_date" .
    " GROUP BY l.limit_id DESC";
    $promote_goods = $GLOBALS['db']->getAll($sql);

    $smarty->assign('expect_end_time', $expect_end_time);
    $smarty->assign('expect_start_time', $expect_start_time);
    $smarty->assign('promote_goods', $promote_goods);
    $smarty->assign('index', "1");
    $smarty->assign('act', $act);
}
/*随机查询推荐商品*/
$sql =  " SELECT goods_id, goods_name , shop_price,promote_price,promote_end_date,promote_start_date,goods_thumb,market_price FROM " . $ecs->table('goods') .
        " WHERE warn_number = 1 AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 ".
        " ORDER BY RAND() LIMIT 30";
$recommend_goods= $db->getAll($sql);
foreach ($recommend_goods as $key => $value) {
   if($value['promote_start_date']<time()&&$value['promote_end_date']>time()&&!empty($value['promote_price'])){
        $recommend_goods[$key]['shop_price']=$value['promote_price'];
   }
}

    $state = 0;
    /*第一个弹框是否出现过*/
    if (empty($_SESSION['prompt_1'])) {
            $state = 1;
            $_SESSION['prompt_1'] = 1;
    }else{
        $state = 0;
    }

    // if ($_SESSION['user_id'] != 12 ) {
    //         $state =0;
    // }
    $smarty->assign('state', $state);
    $smarty->assign('recommend_goods', $recommend_goods);
    $smarty->display("index.dwt", $cache_id);
// }
/**
 * 获得广告位
 *
 * @access  private
 * @return  array
 */
    
function get_ad($position_id = 0){
    if(!$position_id) return false;
    $position_id = intval($position_id);
    $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('ad') . " WHERE position_id = '$position_id' ORDER BY ad_name ASC ";
    return $GLOBALS['db']->getAll($sql);
}

/**
 * 获得分类的信息
 *
 * @param   integer $cat_id
 *
 * @return  void
 */
function get_cat_info($cat_id)
{
    return $GLOBALS['db']->getRow('SELECT cat_id, cat_name, keywords, cat_desc, style, grade, filter_attr, parent_id, layer FROM ' . $GLOBALS['ecs']->table('category') .
        " WHERE cat_id = '$cat_id'");
}

/**
 * 获得三级分类，基本思路是根据层级来判断
 *
 * @param   array $cat_info
 *
 * @return  array
 */
function get_3layer_cat($cat_info){
    /*  如果当前是一级分类，取得当前分类及当前分类的下级分类（二级分类）、下下级分类（三级分类） */
    if($cat_info['layer'] == 1){
        /* 一级分类，即读取与当前分类同级的所有一级分类 */
        $cat_layer1 = $GLOBALS['db']->getAll("SELECT `cat_id`, `cat_name`, `keywords`, `cat_desc`, `style`, `grade`, `filter_attr`, `parent_id`, `layer` FROM  {$GLOBALS['ecs']->table('category')} WHERE `parent_id` = {$cat_info['parent_id']} ORDER BY `sort_order` DESC");
        /* 二级分类 包含 三级分类 */
        $cat_layer2 = $GLOBALS['db']->getAll("SELECT `cat_id`, `cat_name`, `keywords`, `cat_desc`, `style`, `grade`, `filter_attr`, `parent_id`, `layer` FROM  {$GLOBALS['ecs']->table('category')} WHERE `parent_id` = {$cat_info['cat_id']} ORDER BY `sort_order` DESC");
        foreach ($cat_layer2 as $k => $v){
            $cat_layer2[$k]['sub_cat'] = get_child_tree($v['cat_id']);
        }
    } 
    /* 如果当前是二级分类，取得当前分类上级分类（一级分类）、当前分类的下级分类（三级分类） */
    if($cat_info['layer'] == 2){
        /* 二级分类 */
        $cat_layer2 = $GLOBALS['db']->getAll("SELECT `cat_id`, `cat_name`, `keywords`, `cat_desc`, `style`, `grade`, `filter_attr`, `parent_id`, `layer` FROM  {$GLOBALS['ecs']->table('category')} WHERE `parent_id` = {$cat_info['parent_id']} ORDER BY `sort_order` DESC");
        /* 一级分类，即读取与当前父分类同级的所有一级分类 */
        $p_parent_cat_id = $GLOBALS['db']->getOne("SELECT `parent_id` FROM  {$GLOBALS['ecs']->table('category')} WHERE `cat_id` = {$cat_info['parent_id']} LIMIT 1");
        $cat_layer1 = $GLOBALS['db']->getAll("SELECT `cat_id`, `cat_name`, `keywords`, `cat_desc`, `style`, `grade`, `filter_attr`, `parent_id`, `layer` FROM  {$GLOBALS['ecs']->table('category')} WHERE `parent_id` = {$p_parent_cat_id} ORDER BY `sort_order` DESC");
        foreach ($cat_layer2 as $k => $v){
            $cat_layer2[$k]['sub_cat'] = get_child_tree($v['cat_id']);
        }
        /* 是否是当前分类的一级分类，用于一级分类的选中状态 */
        foreach($cat_layer1 as $k => $v){
            if($cat_info['parent_id'] == $v['cat_id']){
                $cat_layer1[$k]['current_layer'] = 1;
            }else{
                $cat_layer1[$k]['current_layer'] = 0;
            }
        } 
    }
    /* 如果当前是第三级分类，取得当前分类及当前分类的上级分类（二级分类）、上上级分类（一级分类） */
    if($cat_info['layer'] == 3){
        /* 二级分类，与当前父级同级的分类 */
        $p_parent_cat_id = $GLOBALS['db']->getOne("SELECT `parent_id` FROM  {$GLOBALS['ecs']->table('category')} WHERE `cat_id` = {$cat_info['parent_id']} LIMIT 1");
        $cat_layer2 =  $GLOBALS['db']->getAll("SELECT `cat_id`, `cat_name`, `keywords`, `cat_desc`, `style`, `grade`, `filter_attr`, `parent_id`, `layer` FROM  {$GLOBALS['ecs']->table('category')} WHERE `parent_id` = $p_parent_cat_id ORDER BY `sort_order` DESC");
        /* 一级分类 与当前父级父级同级的分类 */
        $p_p_parent_cat_id = $GLOBALS['db']->getOne("SELECT `parent_id` FROM  {$GLOBALS['ecs']->table('category')} WHERE `cat_id` = {$p_parent_cat_id} LIMIT 1");
        $cat_layer1 =  $GLOBALS['db']->getAll("SELECT `cat_id`, `cat_name`, `keywords`, `cat_desc`, `style`, `grade`, `filter_attr`, `parent_id`, `layer` FROM  {$GLOBALS['ecs']->table('category')} WHERE `parent_id` = $p_p_parent_cat_id ORDER BY `sort_order` DESC");
        foreach ($cat_layer2 as $k => $v){
            $cat_layer2[$k]['sub_cat'] = get_child_tree($v['cat_id']);
            if($cat_info['parent_id']==$v['cat_id']){
                $cat_layer2[$k]['current_layer'] = 1;
            }else{
                $cat_layer2[$k]['current_layer'] = 0;
            }
        }
        /* 是否是当前分类的一级分类，用于一级分类的选中状态 */
        foreach($cat_layer1 as $k => $v){
            if($p_parent_cat_id==$v['cat_id']){
                $cat_layer1[$k]['current_layer'] = 1;
            }else{
                $cat_layer1[$k]['current_layer'] = 0;
            }
        }
    }
    foreach ($cat_layer1 as $k => $v){
        $cat_layer1[$k]['url']  = build_uri('category', array('cid' => $v['cat_id']), $v['cat_name']);
    }
    foreach ($cat_layer2 as $k => $v){
        $cat_layer2[$k]['url']  = build_uri('category', array('cid' => $v['cat_id']), $v['cat_name']);
    }
    return array(1=>$cat_layer1,2=>$cat_layer2);
}

/**
 * 从缓存文件中获得三级分类，基本思路是根据层级来判断
 *
 * @param   array $cat_info
 *
 * @return  array
 */
function get_3layer_cache_cat($cat_id){
    
    $cat_data = read_static_cache('cat_option_static');
    $cat_info = $cat_data[$cat_id];
    /* $sort_order 用于给二维数据排序，二维数据中的 cat_id 值排序 */
    $cat_layer1 = $cat_layer2 = $sort_order = array();
    
    foreach ($cat_data as $k => $v)  {
        $cat_data[$k]['url'] = build_uri('category', array('cid' => $v['cat_id']), $v['cat_name']);
        if(!$v['is_show']) unset($cat_data[$k]);
    }
    
    if($cat_info['level'] == 1){
        foreach($cat_data as $k => $v){
            if($v['parent_id'] == $cat_info['parent_id']) 
            {    
                $cat_layer1[$k] = $v;
                $sort_order[$k] = $v['cat_id'];
            }
            if($v['parent_id'] == $cat_info['cat_id'])
            {
                $cat_layer2[$k] = $v; 
                foreach ($cat_data as $key => $value) if($value['parent_id'] == $cat_layer2[$k]['cat_id']) $cat_layer2[$k]['sub_cat'][$key] = $value;
            }
        }
    }
    
    if($cat_info['level'] == 2){
        foreach($cat_data as $k => $v){
            if($v['parent_id'] == $cat_data[$cat_info['parent_id']]['parent_id']) 
            {
                $cat_layer1[$k] = $v;
                $sort_order[$k] = $v['cat_id'];
                if( $cat_info['parent_id'] == $v['cat_id']) $cat_layer1[$k]['current_layer'] = 1;
            }
            if($v['parent_id'] == $cat_info['parent_id']) 
            {
                $cat_layer2[$k] = $v;
                foreach ($cat_data as $key => $value) if($value['parent_id'] == $cat_layer2[$k]['cat_id']) $cat_layer2[$k]['sub_cat'][$key] = $value;
            }
        }
    }
    
    if($cat_info['level'] == 3){
        foreach($cat_data as $k => $v){
            if($v['parent_id'] == $cat_data[$cat_data[$cat_info['parent_id']]['parent_id']]['parent_id'])  
            {
                $cat_layer1[$k] = $v;
                $sort_order[$k] = $v['cat_id'];
                if( $cat_data[$cat_info['parent_id']]['parent_id'] == $v['cat_id']) $cat_layer1[$k]['current_layer'] = 1;
            }
            if($v['parent_id'] == $cat_data[$cat_info['parent_id']]['parent_id']) 
            {
                $cat_layer2[$k] = $v;
                foreach ($cat_data as $key => $value) if($value['parent_id'] == $cat_layer2[$k]['cat_id']) $cat_layer2[$k]['sub_cat'][$key] = $value;
            }
        } 
    }    
    
    /* 二维数据 $cat_layer1 排序，按二维数据中第二维的数组中 key 为 cat_id 的值降序 */
    array_multisort( $sort_order,SORT_ASC ,$cat_layer1);
    
    return array(1=>$cat_layer1,2=>$cat_layer2);
}

?>
